extends Node2D

var loginInfo:LoginInfo

func _ready():
	checkVersion()
	writeInEditor()
	$"登录".disabled = true

func _process(_delta):
	if(KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_WARP)):
		editor_wrap()

# 换行
func editor_wrap():
	$"密码/LineEdit".grab_focus()

# 比对客户端版本号和服务器版本号
func checkVersion():
	var headers = ["Content-Type: application/json"]
	var json_str = JSON.stringify({"version":Config.VERSION})
	var error = $"版本号对比Request".request("http://"+ Config.SERVER_ADDRESS +":8080/checkVersion",headers,HTTPClient.METHOD_POST,json_str)
	if(error != 0):
		ObjectManager.toast.showToast("服务器异常")

# 提前写入上次登录的账号密码
func writeInEditor():
	loginInfo = DataStorageManager.load_loginInfo()
	if(loginInfo != null):
		if(loginInfo.is_remember):
			$"账号/LineEdit".text = loginInfo.username
			$"密码/LineEdit".text = loginInfo.password
			$"记住密码".set_pressed_no_signal(loginInfo.is_remember)
		else:
			$"账号/LineEdit".text = ""
			$"密码/LineEdit".text = ""
	else :
		loginInfo = LoginInfo.new()

func _on_login_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	var username:String = $"账号/LineEdit".text
	var password:String = $"密码/LineEdit".text
	if(username.is_empty() || password.is_empty()):
		ObjectManager.toast.showToast("账号密码不能为空")
		return
	var headers = ["Content-Type: application/json"]
	var json_str = JSON.stringify({"username":username,"password":password})
	var error = $"HTTPRequest".request("http://"+ Config.SERVER_ADDRESS +":8080/login",headers,HTTPClient.METHOD_POST,json_str)
	if(error != 0):
		ObjectManager.toast.showToast("服务器异常")

func _on_http_request_request_completed(result, response_code, _headers, byte_arry:PackedByteArray):
	if(result == 0 && response_code == 200):
		var obj = JSON.parse_string(byte_arry.get_string_from_utf8())
		print(obj)
		var code:int = obj.code
		var message:String = obj.message
		match code:
			0:# 登录成功
				print("登录成功")
				# 账号密码信息保存本地
				loginInfo.username = $"账号/LineEdit".text
				loginInfo.password = $"密码/LineEdit".text
				loginInfo.is_remember = $"记住密码".button_pressed
				DataStorageManager.save_loginInfo(loginInfo)
				# 账号信息保存
				var myUserInfo = MyUserInfo.new()
				myUserInfo.username = obj.data.username
				myUserInfo.score = 0
				ObjectManager.myUserInfo = myUserInfo
				ObjectManager.main.jump_to_personalCenter()
			1:# 注册成功
				print("注册成功")
			400:# 登录失败
				pass
			var new:
				print("未知数据 ",new)
		ObjectManager.toast.showToast(message)


func _on_记住密码_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)


func _on_版本号对比request_request_completed(result, response_code, _headers, byte_arry:PackedByteArray):
	if(result == 0 && response_code == 200):
		var obj = JSON.parse_string(byte_arry.get_string_from_utf8())
		var code:int = obj.code
		var message:String = obj.message
		if (code == 0) :
			print("版本号比对成功")
			$"登录".disabled = false
			return
		else :
			ObjectManager.toast.showToast(message)
			await get_tree().create_timer(1).timeout
			get_tree().quit()
			


func _on_登录_mouse_entered() -> void:
	$"登录".scale = Vector2(0.85,0.85)


func _on_登录_mouse_exited() -> void:
	$"登录".scale = Vector2(0.8,0.8)
